<?php

namespace App\Controller\User;


use App\Constants\CodeData;
use App\Controller\BaseController;
use App\Exception\MessageException;
use App\Model\Backpack;
use App\Request\User\AnchorGreetRequest;
use App\Request\User\BaseInfoRequest;
use App\Request\User\ClosenessRequest;
use App\Request\User\EditFriendRequest;
use App\Request\User\ExchangeGiftRequest;
use App\Request\User\FollowListRequest;
use App\Request\User\FollowRequest;
use App\Request\User\PerfectRequest;
use App\Request\User\SetBlacklistRequest;
use App\Request\User\SignRequest;
use App\Services\Blacklist\BlacklistService;
use App\Services\GoldCoin\ExchangeService;
use App\Services\Sign\SignService;
use App\Services\User\FollowService;
use App\Services\User\UserService;
use Hyperf\Context\Context;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface;

class UserController extends BaseController
{
    #[Inject]
    protected UserService $userService;

    #[Inject]
    protected FollowService $followService;

    #[Inject]
    protected ExchangeService $exchangeService;

    #[Inject]
    protected SignService $signService;

    #[Inject]
    protected BlacklistService $blacklistService;

    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;


    /**
     * Note - 用户详情页接口,
     * DateTime 2024/7/15 17:04
     * User Admin
     */
    public function detail()
    {
        $user = Context::get('user');
        $language = Context::get('language', 'en');
        $uid = $this->request->post('uid', 0);
        $userId = $this->request->post('user_id', 0);
        if (!$userId) {
            throw new MessageException('参数错误');
        }
        $details = $this->userService->getUserDetail($user, $uid, $userId, $language);
        return $this->return($details);

    }

    /**
     * Note - 用户详情页接口,其他数据(照片墙，礼物墙)
     * DateTime 2024/7/15 17:05
     * User Admin
     */
    public function detailOther()
    {
        $user = Context::get('user');
        $language = Context::get('language', 'en');
        $uid = $this->request->post('uid', 0);
        $userId = $this->request->post('user_id', 0);
        if (!$userId) {
            throw new MessageException('参数错误');
        }

        $otherDetails = $this->userService->getUserDetailOther($user, $uid, $userId, $language);
        return $this->return($otherDetails);
    }


    /**
     * Note - 获取相册列表,及其是否被访问过
     * DateTime 2024/7/16 12:17
     * User Admin
     * @return ResponseInterface
     */
    public function photos()
    {
        $user = Context::get('user');
        $uid = $this->request->post('uid', 0);
        $page = $this->request->post('page', 1);
        // 1-查看自己的相册； 2-查看他人的相册
        $type = $this->request->post('type', 1);

        $from = $this->request->post('from', '');
        $version_code = $this->request->post('version_code', '');
        $channel = $this->request->post('channel', '');

        // 被访问者用户ID
        $userId = $this->request->post('userId', 0);

        //根据type重构查询的user_id
        $userId = $type == 1 ? $uid : $userId;

        $photos = $this->userService->getUserPhotos($user, $userId, $type, $page, 50, $from, $version_code, $channel);
        return $this->return($photos);

    }


    /**
     * Note - 获取视频列表,及其是否被访问过
     * DateTime 2024/7/17 10:14
     * User Admin
     * @return ResponseInterface
     */
    public function videos()
    {
        $user = Context::get('user');
        $uid = $this->request->post('uid', 0);
        $page = $this->request->post('page', 1);
        // 1-查看自己的视频； 2-查看他人的视频
        $type = $this->request->post('type', 1);

        $from = $this->request->post('from', '');
        $version_code = $this->request->post('version_code', '');
        $channel = $this->request->post('channel', '');

        // 被访问者用户ID
        $userId = $this->request->post('userId', 0);
        //根据type重构查询的user_id
        $userId = $type == 1 ? $uid : $userId;


        $photos = $this->userService->getUserVideos($user, $userId, $type, $page, 50, $from, $version_code, $channel);
        return $this->return($photos);
    }

    /**
     * Note - 征友条件列表获取和编辑征友条件
     * DateTime 2024/7/17 10:31
     * User Admin
     */
    public function aboutFriend(EditFriendRequest $request)
    {
        $params = $this->request->post();
        $action = $params['action'] ?? 'index';
        $user = Context::get('user');
        switch ($action) {
            case 'index':
                $aboutFriend = $this->userService->getAboutFriendList($user['id']);
                return $this->return($aboutFriend);
            case 'edit':
                $request->execute($params, 'edit');
                $this->userService->editAboutFriend($user['id'], $params);
                return $this->return([
                    'code' => 200,
                    'desc' => 'success',
                    'data' => ['result' => 1]
                ]);
            default:
                return $this->return(formatData(['code' => 500, 'desc' => '参数错误']));
        }
    }


    /**
     * Note - 主页停留，主播自动打招呼
     * DateTime 2024/7/17 14:12
     * User Admin
     */
    public function anchorGreet(AnchorGreetRequest $request)
    {
        $user = Context::get('user');
        $params = $this->request->post();
        $request->execute($params);
        $params['language'] = Context::get('language', 'en');;
        $result = $this->userService->anchorGreet($user, $params);

        return $this->return($result);
    }


    /**
     * Note - 获取我的背包列表
     * DateTime 2024/7/17 17:30
     * User Admin
     * @return ResponseInterface
     */
    public function backpack()
    {
        $user = Context::get('user');
        $backpack = Backpack::where('uid', $user['id'])->where('num', '>', 0)->whereIn('type', [2, 3])->get()->toArray();
        return $this->return([
            'code' => 200,
            'desc' => 'success',
            'data' => ['result' => 1, 'msg' => $backpack]
        ]);

    }

    /**
     * Note - 用户基本信息接口
     * DateTime 2024/7/17 18:14
     * User Admin
     * @return ResponseInterface
     */
    public function baseInfo(BaseInfoRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $user = Context::get('user');
        $userId = $params['user_id'] ?? 0;
        $fromPage = $params['from_page'] ?? 0;
        $language = Context::get('language', 'en');
        $baseInfo = $this->userService->getBaseInfo($user, $userId, $fromPage, $language);
        return $this->return($baseInfo);
    }

    /**
     * Note - 获取用户关系亲密度数据接口
     * DateTime 2024/7/18 16:41
     * User Admin
     * @param ClosenessRequest $request
     * @return ResponseInterface
     */
    public function closeness(ClosenessRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userId = $params['user_id'] ?? 0;
        $user = Context::get('user');
        $language = Context::get('language', 'en');
        $closeness = $this->userService->getCloseness($user, $userId, $language);
        return $this->return($closeness);
    }


    /**
     * Note - 修改用户信息
     * DateTime 2024/8/7 17:30
     * User Admin
     * @return ResponseInterface
     */
    public function editInfo()
    {
        $user = Context::get('user');
        $canCancelAccount = $this->userService->editInfo($user, $this->request->post());
        return $this->return($canCancelAccount);
    }


    /**
     * Note - 完善个人资料+绑定公会+设置收费
     * DateTime 2024/8/7 18:30
     * User Admin
     * @param PerfectRequest $request
     * @return ResponseInterface
     */
    public function perfect(PerfectRequest $request)
    {
        $user = Context::get('user');
        $params = $this->request->post();
        $request->execute($params);
        $data = Db::transaction(function () use ($user, $params) {
            $update = $this->userService->perfect($user['id'], $params);
            if (!empty($update)) {
                return formatData(['result' => 1, 'msg' => $update]);
            } else {
                return formatData('', 200, CodeData::NEED_TIPS, '修改失败');
            }
        });
        return $this->return($data);

    }


    /**
     * Note - 我的关注列表，粉丝列表，互粉列表，密友列表
     * DateTime 2024/8/9 18:20
     * User Admin
     * @return ResponseInterface
     */
    public function followList(FollowListRequest $request)
    {
        $user = Context::get('user');
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = $this->followService->followList($user, $params);
        return $this->return($userInfo);
    }


    /**
     * Note - 关注用户与取消关注
     * DateTime 2024/8/9 18:20
     * User Admin
     * @return ResponseInterface
     */
    public function follow(FollowRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $user = Context::get('user');
        $follow = $this->followService->follow($user, $params);
        return $this->return($follow);

    }


    /**
     * Note - 查询兑换金币
     * DateTime 2024/8/12 10:45
     * User Admin
     * @return ResponseInterface
     */
    public function exchangeGiftCount(ExchangeGiftRequest $request)
    {
        $user = Context::get('user');
        $params = $this->request->post();
        $params['language'] = Context::get('language', 'en');
        // 1=查询积分、金币；2=兑换金币
        $type = $params['type'];
        // $request = $this->container->get(ExchangeGiftRequest::class);
        if ($type == 1) {
            // $request->scene('query')->validateResolved();
            $request->execute($params, 'query');
            $queryGiftCount = $this->exchangeService->queryGiftCount($user['id'], $params);
            return $this->return($queryGiftCount);
        } elseif ($type == 2) {
            // $request->scene('exchange')->validateResolved();
            $request->execute($params, 'exchange');
            $exchangeGiftCount = $this->exchangeService->exchangeGiftCount($user['id'], $params);
            return $this->return($exchangeGiftCount);
        }
    }


    /**
     * Note - 签到列表(初始化签到数据)
     * DateTime 2024/8/12 12:29
     * User Admin
     * @return ResponseInterface
     */
    public function signList()
    {
        $user = Context::get('user');
        $language = Context::get('language', 'en');
        $signList = $this->signService->signList($user['id'], $language);
        return $this->return($signList);
    }


    /**
     * Note - 签到
     * DateTime 2024/8/12 12:57
     * User Admin
     * @return ResponseInterface
     */
    public function sign(SignRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $user = Context::get('user');
        $language = Context::get('language', 'en');
        $sign = $this->signService->sign($user['id'], $language, $params);
        if ($sign) {
            return $this->return($sign);
        } else {
            return $this->fail(formatData('', 200, CodeData::NEED_TIPS, '签到失败'));
        }

    }


    /**
     * Note - 用户黑名单列表
     * DateTime 2024/8/12 19:36
     * User Admin
     * @return ResponseInterface
     */
    public function blackList()
    {
        $user = Context::get('user');
        $blackList = $this->blacklistService->blackList($user['id']);
        return $this->return($blackList);

    }


    /**
     * Note - 设置、解除黑名单用户
     * DateTime 2024/8/12 19:39
     * User Admin
     */
    public function setBlacklist(SetBlacklistRequest $request)
    {
        $user = Context::get('user');
        $uid = $user['id'];
        $params = $this->request->post();
        $request->execute($params);
        $type = $params['type'] ?? 1; //1:设置黑名单 2:当前拉黑的解除；被解除的,再次拉黑
        $black_uid = $params['black_uid'] ?? 0;
        $setBlacklist = $this->blacklistService->setUserBlacklist($uid, $black_uid, $type);
        return $this->return($setBlacklist);

    }


}